﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Home.MobileSales.Common.Entities;
using OrderSales.Interface;
using OrderSales.Presenter;

namespace Home.MobileSales.Sales.View
{
    public partial class OrderForm : Form, IOrderView
    {
        #region IOrderForm Members

        public string NumberOrder
        {
            get
            {
                return numberOrderTextBox.Text;
            }
            set
            {
                numberOrderTextBox.Text = value;
            }
        }

        public DateTime DateOrder
        {
            get
            {
                return dateOrderDateTimePicker.Value;
            }
            set
            {
                dateOrderDateTimePicker.Value = value;
            }
        }

        public double Amount
        {
            set { totalAmountLabel.Text = String.Format("Сума {0} грн.", value.ToString()); }
        }

        public List<OrderRecord> RecordOrders
        {
            set
            {
                orderRecordDataGrid.DataSource = value;
            }
        }

        public object DataSource
        {
            set
            {
                orderRecordDataGrid.DataSource = value;
                recordOrderdataGridTableStyle.MappingName = orderRecordDataGrid.DataSource.GetType().Name;
            }
            get
            {
                return orderRecordDataGrid.DataSource;
            }
        }

        public object Current
        {
            get
            {
                return orderRecordDataGrid.CurrentRowIndex;
            }
        }

        public BuyPoint OrderBuyPoint
        {
            set
            {
                buyerTextBox.Text = value.Buyer.NameBuyer;
                buyPointTextBox.Text = value.NameBuyPoint;
                adressLabel.Text = value.Adress;
            }
        }

        public object CurrentSklad
        {
            get { return SkladComboBox.SelectedItem; }
            set { SkladComboBox.SelectedValue = value; }
        }

        public List<Sklad> Sklads
        {
            set
            {
                SkladComboBox.DataSource = value;
                SkladComboBox.ValueMember = "IdSklad";
                SkladComboBox.DisplayMember = "NameSklad";
            }
        }

        public bool IsVan
        {
            get
            {
                return vanCheckBox.Checked;
            }
            set
            {
                vanCheckBox.Checked = value;
            }
        }


        public event EventHandler<EventArgs> AddRecordOrder;

        public event EventHandler<EventArgs> RemoveRecordOrder;

        public event EventHandler<EventArgs> ChangeOrder;

        public event EventHandler<EventArgs> PrintOrder;


        #endregion

        public OrderForm(Order order, BuyPoint buyPoint, bool editable)
        {
            InitializeComponent();
            new OrderFormPresenter(order, buyPoint, this);
            orderRecordDataGrid.RowHeadersVisible = false;
            detailsPanel.Enabled = editable;
            addGoodMenuItem.Enabled = editable;
            removeOrderRecordMenuItem.Enabled = editable;
            Cursor.Current = Cursors.Default;
        }

        private void addGoodMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            AddRecordOrder(null, null);
        }

        private void orderRecordDataGrid_CurrentCellChanged(object sender, EventArgs e)
        {
            if (orderRecordDataGrid.CurrentRowIndex >= 0)
                printOrderMenuItem.Enabled = true;
            else
            {
                printOrderMenuItem.Enabled = false;
            }
        }

        private void removeGoodMenuItem_Click(object sender, EventArgs e)
        {
            RemoveRecordOrder(null, null);
        }

        private void changeOrder(object sender, EventArgs e)
        {
            if (ChangeOrder != null)
            {
                ChangeOrder(null, null);
            }
        }

        private void contextMenu_Popup(object sender, EventArgs e)
        {

        }

        private void printOrderMenuItem_Click(object sender, EventArgs e)
        {
            PrintOrder(null, null);
        }
    }
}